package com.enbatis.mybatisplugs.service.impl;

import com.enbatis.mybatisplugs.base.Account;
import com.enbatis.mybatisplugs.base.BaseServiceImpl;

import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.IdWorker;
import com.enbatis.mybatisplugs.entity.*;
import com.enbatis.mybatisplugs.mapper.TSalesMapper;
import com.enbatis.mybatisplugs.service.TCustomerService;
import com.enbatis.mybatisplugs.service.TProductService;
import com.enbatis.mybatisplugs.service.TSalesItemService;
import com.enbatis.mybatisplugs.service.TSalesService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.*;

/**
* 描述：
* @author wangweidong
* @date 2019-07-04 19:08:41
*/

@Service
public class TSalesServiceImpl extends BaseServiceImpl<TSalesMapper,TSales > implements TSalesService {

    @Autowired
    private TCustomerService tCustomerService;

    @Autowired
    private TProductService tProductService;

    @Autowired
    private TSalesItemService tSalesItemService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addSales(Long purchaseId, Integer num, Long[] customerIds, Account sysUser) {

        List<TCustomer> customerList=tCustomerService.list(new Wrapper<>(new TCustomer()).setSqlSelect("id","name").in("id",customerIds));
        Map<Long,TCustomer> customerMap=new HashMap<>();
        for (TCustomer customer:customerList) {
            customerMap.put(customer.getId(),customer);
        }
        TProduct tProduct= tProductService.getById(purchaseId);
        BigDecimal total=tProduct.getPrice().multiply(new BigDecimal(num));
        Map<Long,TSales> salesHashMap=new HashMap<>();
        List<TSales> salesList=list(new Wrapper<>(new TSales()).in("customer_id",customerIds).eq("status",0));
        for ( TSales sales:salesList) {
            salesHashMap.put(sales.getCustomerId(),sales);
        }
        List<TSales> saveList=new ArrayList<>();
        List<TSalesItem> saveItemList=new ArrayList<>();
        for (Long customerId:customerIds) {
             if (null==salesHashMap.get(customerId)){
                 TSales tSales=new TSales();
                 tSales.setCustomer(customerMap.get(customerId).getName());
                 tSales.setCustomerId(customerId);
                 tSales.setUname(sysUser.getUsername());
                 tSales.setCode("SALES"+new Random().nextInt(4));
                 tSales.setCompanyId(sysUser.getCompanyId());
                 tSales.setCtime(new Date());
                 tSales.setUid(sysUser.getId());
                 tSales.setDeleted(0);
                 tSales.setStatus(0);
                 tSales.setSalesTime(new Date());
                 tSales.setCost(total);
                 tSales.setId(IdWorker.getId());
                 saveList.add(tSales);

                 TSalesItem tSalesItem=new TSalesItem();
                 tSalesItem.setCompanyId(sysUser.getCompanyId());
                 tSalesItem.setCtime(new Date());
                 tSalesItem.setNum(num);
                 tSalesItem.setProductId(purchaseId);
                 tSalesItem.setPrice(tProduct.getPrice());
                 tSalesItem.setSalesId(tSales.getId());
                 tSalesItem.setUid(sysUser.getId());
                 tSalesItem.setDeleted(0);
                 tSalesItem.setUname(sysUser.getUsername());
                 saveItemList.add(tSalesItem);

             }else{

                TSales sales=new TSales();
                sales.setId(salesHashMap.get(customerId).getId());
                sales.setCost(salesHashMap.get(customerId).getCost().add(total));
                 sales.setLid(sysUser.getId());
                 sales.setLtime(new Date());
                 updateById(sales);
                 TSalesItem tSalesItem=new TSalesItem();
                 tSalesItem.setCompanyId(sysUser.getCompanyId());
                 tSalesItem.setCtime(new Date());
                 tSalesItem.setNum(num);
                 tSalesItem.setProductId(purchaseId);
                 tSalesItem.setPrice(tProduct.getPrice());
                 tSalesItem.setSalesId(sales.getId());
                 tSalesItem.setUid(sysUser.getId());
                 tSalesItem.setDeleted(0);
                 tSalesItem.setUname(sysUser.getUsername());
                 saveItemList.add(tSalesItem);

             }


        }
        if (CollectionUtils.isNotEmpty(saveList)){
            saveBatch(saveList);
        }
        if (CollectionUtils.isNotEmpty(saveItemList)){
            tSalesItemService.saveBatch(saveItemList);
        }
        return true;
    }

    @Override
    public boolean salesCancel(Long[] ids, Account sysUser) {

        TSales sales=new TSales();
        sales.setStatus(-1);
        sales.setLid(sysUser.getId());
        sales.setLtime(new Date());
        return super.update(sales,new Wrapper<>(new TSales()).in("id",ids))>0;
    }

    @Override
    public boolean salesEnd(Long[] ids, Account sysUser) {

        TSales sales=new TSales();
        sales.setStatus(1);
        sales.setLid(sysUser.getId());
        sales.setLtime(new Date());
        return super.update(sales,new Wrapper<>(new TSales()).in("id",ids))>0;
    }
}
